package com.ship.contract.utils;


import com.ship.common.core.utils.StringUtils;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.xwpf.usermodel.*;


import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.UUID;

/**
 * @author tangshixuan
 * @date 2023-9-27
 */
public class FileUtil {
    public static Boolean getIsScanPdf(String path) {
        String tempFilePath = System.getProperty("java.io.tmpdir") + File.separator + UUID.randomUUID().toString();
        String text = null;
        PDDocument document = null;
        File pdfFile = null;

        String CsoldPart = "http://10.75.34.224:9300/statics";
        String ZsoldPart = "http://10.75.33.149:9300/statics";
        String CsPart = "https://lngshippingcs.cgp.cnooc/api/file/statics";
        String ZsPart = "https://lngshipping.cgp.cnooc/api/file/statics";

            String newUrl = "";
            if (path.contains("https://lngshipping.cgp.cnooc")) {
                newUrl = path.replace(ZsPart, ZsoldPart);
            }else if (path.contains("https://lngshippingcs.cgp.cnooc")){
                newUrl = path.replace(CsPart, CsoldPart);
            }else {
                newUrl = path;
            }

        try {
            // 下载PDF文件
            URL url = new URL(newUrl);
            InputStream inputStream = url.openStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            pdfFile = new File(tempFilePath + ".pdf");
            FileOutputStream fileOutputStream = new FileOutputStream(pdfFile);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
            fileOutputStream.close();
            bufferedInputStream.close();
            // 读取PDF文本内容
            document = PDDocument.load(pdfFile);
            PDFTextStripper stripper = new PDFTextStripper();
            text = stripper.getText(document).replaceAll("\r\n", "");
        } catch (Exception e) {
        } finally {
            try {
                if (document != null) {
                    document.close();
                }
                if (pdfFile != null) {
                    pdfFile.delete();
                }
            } catch (IOException e) {
                // 处理关闭文件或删除文件时的异常
            }
        }
        if (StringUtils.isBlank(text) || text.trim().isEmpty()) {
            return true;
        }
        return false;
    }

    public static MultipartFile getNotScanPdf(String path) {
        String tempFilePath = System.getProperty("java.io.tmpdir") + File.separator + UUID.randomUUID().toString();
        String text = null;
        PDDocument document = null;
        File pdfFile = null;
        try {
            // 下载PDF文件
            URL url = new URL(path);
            InputStream inputStream = url.openStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            pdfFile = new File(tempFilePath + ".pdf");
            FileOutputStream fileOutputStream = new FileOutputStream(pdfFile);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
            fileOutputStream.close();
            bufferedInputStream.close();
            // 读取PDF文本内容
            document = PDDocument.load(pdfFile);

            long time = System.currentTimeMillis();
            String fileName = time + ".pdf";


            MultipartFile file = convert(pdfFile, fileName);
            return file;
        } catch (Exception e) {

        }
        return null;
    }

    public static MultipartFile convert(File file, String fileName) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] byteArray = byteArrayOutputStream.toByteArray();

        // 使用Spring框架的MockMultipartFile来创建MultipartFile实例
        MultipartFile multipartFile = new MockMultipartFile(fileName, fileName, "application/pdf", byteArray);
        byteArrayOutputStream.close();
        return multipartFile;
    }

}
